home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
IRIX 6.5 Applications 2002 November
/
SGI IRIX 6.5 Applications 2002 November.iso
/
dev
/
insight_dev.idb
/
usr
/
share
/
Insight
/
bin
/
make_bldsgml.z
/
make_bldsgml
Wrap
Text File
|
2002-10-15
|
14KB
|
467 lines
#!/usr/bin/perl5
#
# Copyright 2002, Silicon Graphics, Inc.
# All Rights Reserved.
#
# This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
# the contents of this file may not be disclosed to third parties, copied or
# duplicated in any form, in whole or in part, without the prior written
# permission of Silicon Graphics, Inc.
#
# RESTRICTED RIGHTS LEGEND:
# Use, duplication or disclosure by the Government is subject to restrictions
# as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
# and Computer Software clause at DFARS 252.227-7013, and/or in similar or
# successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
# rights reserved under the Copyright Laws of the United States.
#
###############################################################################
#
# Creates buildable SGML source from parent SGML file and components.
# Called by commondocrules for "make book" operation.
#
# Requires perl5 to run correctly.
$| = 1;
################################################################################
### Subroutine to run system command lines, check for errors, & issue messages
### $1 - $n = The system command lines to run
sub RunIt {
my($CmdLine) = @_;
my($ECode) = 0;
$ECode = system ("$CmdLine");
if ($ECode) {
if ($ECode > 255) { $ECode /= 256 ; }
print STDERR "\a\n$Name: $ECode from:\n\t$CmdLine\n" ;
exit($ECode);
}
}
################################################################################
### Cleans up SGML files: Removes Adept processing instructions and joins
### tags broken across lines
### $1 = Name of file
### $2 = String containing one complete SGML file
sub CleanUp {
my ($FileName, $Buff) = @_ ;
my (@Tmp, $Ent, %Tmp, @Ents, $Tmp) ;
### change the \r\n output from spam into single carriage-returns
$$Buff =~ s\r\r\n\ng ; ## sometimes two \r passthru by spam
$$Buff =~ s\r\n\ng ;
$$Buff =~ s\rg ;
### Join line-broken tags:
## SGML allows this: <section
## id="thing1"
## >
## But processing in scripts simplified if < to > on one line.
while ($$Buff =~ s#(<[^>]+)[\n]#$1 #g)
{ $NoOp = 1 ; }
### Delete all SGML-standard comments:
$$Buff =~ s#<!--.*?-->##gs;
$$Buff =~ s#<!>##gs ;
### Change all carriage-return processing instructions to newline elements:
$$Buff =~ s#<\?Pub\s*_(newline>)#<$1#g ;
### Remove Adept processing instructions:
$$Buff =~ s#\n<\?[^>]+>\n#\n#g ;
$$Buff =~ s#<\?[^>]+>##g ;
### Fix <stuff> problem for mkbook parser:
## first change all <sgmltag> to SGML_TAGOsgmltagSGML_TAGC :
$$Buff =~ s#<([^>]*)>#SGML_TAGO$1SGML_TAGC#gs ;
## change leftover > chars to > :
$$Buff =~ s#>#>#gs ;
## change delimiters back:
$$Buff =~ s#SGML_TAGO#<#gs ;
$$Buff =~ s#SGML_TAGC#>#gs ;
# todo: verify this is needed
### Change certain character entities to literals (stylesheet bugs):
$$Buff =~ s–-gs ;
$$Buff =~ s—--gs ;
$$Buff =~ s gs ;
$$Buff =~ s…...gs ;
### Change entityref values to include .gif extension:
## Might have <graphic entityref="abc" magnification="90"></graphic>
## or have <graphic magnification="90" entityref="abc"></graphic>
$$Buff =~ s#<graphic([^>]*)entityref\s*=\s*\"([^"]*)\"#<graphic$1entityref=\"$2.gif\"#igs ;
## Adept editor makes specific breaks around inline tags within
## a literal block when line wraps are inserted due to the
## outputrecordlength (orl) setting. Reverse these changes
## to ensure the proper formatting for other processing applications
my($data) = $$Buff;
$$Buff = '';
while($data =~ m#<(programlisting|screen|literallayout|synopsis)([^>]*>)(.*?)(</\1>)#ms) {
$$Buff .= $`;
$literal = '<' . $1 . $2 . $3 . $4;
$data = $';
# If a close tag starts a line within a literal block
# move it back to the end of the previous line.
$literal =~ s#([^\n])\n</#$1</#msg;
$literal =~ s#([^\n])\n\n</#$1\n</#msg;
# If an open tag ends a line within a literal block
# move the next line to follow immediately after
$literal =~ s#(<[^/][^>]*?>)\n#$1#msg;
$$Buff .= $literal;
}
$$Buff .= $data;
}
################################################################################
### Returns content of a tag pair
### $1 = Array index at which to start looking
### $2 = tag name (SGML requires case-insensitive match)
### $3 = Array in which to search
sub GetTagContent {
my ($I, $Tag, $Buff) = @_ ;
my ($J, $Content, ) ;
while ( ($$I <= $#$Buff) && ($$Buff[$$I] !~ m<$$Tag\bi) )
{
$$I++ ;
}
$J = $$I ;
while ( ($J <= $#$Buff) && ($$Buff[$J] !~ m</$$Tag\bi) )
{
$J++ ;
}
if ($$I == $J)
{
$Content = $$Buff[$$I] ;
}
else
{
$Content = join (' ', @$Buff[$$I..$J]) ;
}
$Content =~ s/\n/ /g ;
$Content =~ s^.*<$$Tag[^>]*>(.*)</$$Tag.*$1i ;
$Content =~ s/ +/ /g ;
$Content =~ s/^ +// ;
$Content =~ s/ +$// ;
return ($Content) ;
}
################################################################################
### Returns value for an attribute of an element
### $1 = Array index at which to start looking
### $2 = tag name (SGML requires case-insensitive match)
### $3 = attribute name (SGML requires case-insensitive match)
### $4 = Array in which to search
sub GetAttValue {
my ($I, $Tag, $AttribName, $Buff) = @_ ;
my ($J, $TagContent, $AttValue) ;
### Find the element (tag might be lowercase or uppercase)
while ( ($$I <= $#$Buff) && ($$Buff[$$I] !~ m<$$Tag\bi) )
{
$$I++ ;
}
$J = $$I ;
while ( ($J <= $#$Buff) && ($$Buff[$J] !~ m</$$Tag\bi) )
{
$J++ ;
}
if ($$I == $J)
{
$TagContent = $$Buff[$$I] ;
}
else
{
$TagContent = join (' ', @$Buff[$$I..$J]) ;
}
$TagContent =~ s/\n/ /g ;
$TagContent =~ s^.*<$$Tag([^>]*)>.*$1i ;
($AttValue = $TagContent) =~ s^.*\s$$AttribName\s*=\s*\"([^"]*)\".*$1i ;
return ($AttValue) ;
}
################################################################################
### Gets information from book SGML file
### $1 = Book name; returned by this routine
### $2 = Complete publication number; returned by this routine
### $3 = Book's version; returned by this routine
### $4 = Book's Title; returned by this routine
### $5 = Book's shorttitle attribute; returned by this routine
### $6 = Book's bookshelf attribute; returned by this routine
### $7 = Array of lines from text of book file
sub GetBookInfo {
my ($BookName, $PartNo, $Version, $Title, $ShortTitle,
$BookShelf, $BuffSGML) = @_ ;
my ($ClassCode, $Base, $Answer, $I, $AttribName, $Tag,
$PartInfo, $Tmp, @Tmp, $ChapSuffix) ;
undef $$BookName ;
undef $$PartNo ;
undef $ClassCode ;
undef $Base ;
undef $$Version ;
undef $$ShortTitle ;
undef $$BookShelf ;
## DRD Future version:
## Need to check value in the SGML file against the one in the Makefile. If not
## the same, ask user if the value in the Makefile should be used.
## Default = yes (need to be able to use Makefile to override values in the SGML file.
if (grep (/<partnumber\b/i, @$BuffSGML))
{
$I = 0 ;
$Tag = 'partnumber' ;
$PartInfo = &GetTagContent (\$I, \$Tag, $BuffSGML) ;
$I = 0 ;
$Tag = 'partnumber' ;
$AttribName = 'shorttitle' ;
$$ShortTitle = &GetAttValue (\$I, \$Tag, \$AttribName, $BuffSGML) ;
($ClassCode = $PartInfo) =~ s^.*<classcode[^>]*>(.*)</classcode>.*$\1i ;
($Base = $PartInfo) =~ s^.*<base[^>]*>(.*)</base>.*$\1i ;
($$Version = $PartInfo) =~ s^.*<version[^>]*>(.*)</version>.*$\1i ;
$$PartNo = "${ClassCode}-${Base}-$$Version" ;
}
else
{
print "\n$Name: Only for processing entire book! \n" ;
exit(1);
}
## Get "title" (full title)
$I = 0 ;
$Tag = 'title' ;
$$Title = &GetTagContent (\$I, \$Tag, $BuffSGML) ;
$$Title =~ s/\&[^;]+;//g ; ### Remove any character entities
$$BookName = $$PartNo ;
}
################################################################################
##### MAIN #####
################################################################################
$[ = 0 ;
($Name = $0) =~ s^.*/ ;
select((select(STDOUT), $| = 1)[0]) ; ### No buffering of stdout for messages
$Usage = "
Usage: $Name [-draft] [-numheads] -I<inputfile> -O<outputfile>
-draft
Draft version; keep the comments and revision indicators
for reviewers in displayed version (InSight or the
'post4review' DynaWeb copies).
-numheads
Inserts the numheads=Y attribute on the <sgidocbk>
tag for use by the stylesheets
<inputfile>
Name of the .tmp SGML file generated by 'spam' in the
current book's working directory.
<outputfile>
Name of the final buildable source file, by convention
the <shorttitle>.sgml name.
Example:
$Name -draft -IFSafe_IG.sgml.tmp -OFSafe_IG.sgml
" ;
## definitions
$DraftVersion = 0 ; ## flag for draft or final (default is final; strip <comment> elements)
$NumberedHeads = 0;
$Indexer = "$ENV{'TOOLROOT'}/usr/share/Insight/bin/indexgen_sgidocbk";
$XformSGML_public = "$ENV{'TOOLROOT'}/usr/share/Insight/bin/xformsgml_public";
$TmpFile = "tmp_make_bldsgml.$$" ; ## temporary file(s) created and deleted
## by this script
if (! @ARGV) {
print STDERR "$Usage";
exit(1);
}
while (@ARGV) {
$Arg = shift (@ARGV) ;
if ("$Arg" eq '-draft')
{
$DraftVersion = 1 ;
}
elsif ($Arg eq '-numheads')
{
$NumberedHeads = 1;
}
elsif ($Arg =~ /^-I.+/)
{
$Arg =~ s/^-I// ;
push (@Files, "$Arg") ;
$InputFile = $Arg ;
}
elsif ($Arg =~ /^-O.+/)
{
$Arg =~ s/^-O// ;
push (@Files, "$Arg") ;
$OutputFile = $Arg ;
}
elsif ($Arg =~ /^-/) {
print STDERR "\n\aUnrecognized option, '$Arg'$Usage" ;
exit(1);
}
}
if ("$InputFile" eq '') {
print STDERR "$Usage" ;
exit(1);
}
if ("$OutputFile" eq '') {
print STDERR "$Usage" ;
exit(1);
}
if (! @Files) {
print STDERR "$Usage" ;
exit(1);
}
if (! $DraftVersion) {
print "\nPreparing buildable source SGML for FINAL version.\n";
} else {
print "\nPreparing buildable source SGML for DRAFT version.\n";
}
### Clean up the SGML instance:
print "Preparing SGML instance for processing.\n" ;
if (! open (FileI, "$InputFile")) {
print STDERR "\a\n$Name: $InputFile\n" ;
exit(16);
}
$Buff = join('', <FileI>);
close (FileI) ;
$FileName = "$InputFile" ;
&CleanUp (\$FileName, \$Buff) ;
if (! (open (FileZ, "> $TmpFile"))) {
print STDERR "\a\n$Name: $TmpFile\n" ;
exit(13);
}
print FileZ join ('', $Buff) ;
close (FileZ) ;
### Make public version of file if not making draft version.
## Replace TmpFile with public version.
if (! $DraftVersion) {
$ErrorCode = system ("$XformSGML_public -I$TmpFile -O$TmpFile.2");
if (! $ErrorCode) {
system ("rm -f $TmpFile");
system ("mv $TmpFile.2 $TmpFile");
}
}
### Build the writer's (back-of-book) index:
## (This is specific for Insight.)
print "Generating book index.\n" ;
&RunIt ("$Indexer -i $TmpFile -o $TmpFile.idx") ;
### Put generated index into SGML instance:
print "Integrating book index.\n";
## Get the book info from the SGML instance:
if (! open (FileA, "$TmpFile")) {
print STDERR "\a\n$Name: $TmpFile\"\n" ;
exit(16);
}
undef $/ ;
@BuffSGML = <FileA> ;
close (FileA) ;
$/ = "\n" ;
&GetBookInfo (\$BookName, \$PartNo, \$Version, \$Title,
\$ShortTitle, \$BookShelf, \@BuffSGML) ;
### Compare shorttitle from SGML with value from Makefile:
($InputShortTitle = $OutputFile) =~ s#(.*).sgml#\1#;
if ("$InputShortTitle" ne "$ShortTitle")
{
print "\n\nCAUTION! The short title in the Makefile is different" ;
print "\nthan the value in the SGML file." ;
print "\n Short title from Makefile = $InputShortTitle " ;
print "\n Short title from SGML = $ShortTitle " ;
print "\nContinuing, but using value from Makefile.\n\n" ;
}
### Open the "indexless" SGML instance:
if (! open (FileS, "$TmpFile")) {
print STDERR "\a\n$Name: $TmpFile.2\"\n" ;
exit(16);
}
$Buff1 = join('', <FileS>);
close (FileS);
## Open the index file:
if (! open (FileB, "$TmpFile.idx")) {
print STDERR "\a\n$Name: $TmpFile.idx\"\n" ;
exit(16);
}
$Buff2 = join('', <FileB>);
close (FileB);
### Insert the index into the SGML file:
$Buff1 =~ s#<index>\s*</index>#$Buff2#i;
### Change shorttitle attribute value to Makefile value:
$Buff1 =~ s#<partnumber([^>]*)shorttitle\s*=\s*\"([^"]+)\"#<partnumber$1shorttitle=\"$InputShortTitle\"#ig ;
# add the NUMHEADS attribute to <sgidocbk> if needed
if($NumberedHeads == 1) {
$Buff1 =~ s/(<sgidocbk)/\1 NUMHEADS="Y"/i;
}
## Open the final shorttitle.sgml output file:
if (! (open (FileZ, "> $OutputFile"))) {
print STDERR "\a\n$Name: $Outputfile\n" ;
exit(13);
}
print FileZ $Buff1;
close (FileZ);
### Remove temporary files:
unlink "$TmpFile" ;
unlink "$TmpFile.idx" ;
print "Buildable source SGML done.\n\n" ;